package fr.sedoo.sos.xml;

import java.util.Iterator;

import net.opengis.gml.CodeType;
import net.opengis.gml.PointType;
import net.opengis.gml.StringOrRefType;
import net.opengis.sensorML.x101.ComponentsDocument.Components;
import net.opengis.sensorML.x101.ComponentsDocument.Components.ComponentList;
import net.opengis.sensorML.x101.ComponentsDocument.Components.ComponentList.Component;
import net.opengis.sensorML.x101.IdentificationDocument.Identification;
import net.opengis.sensorML.x101.IdentificationDocument.Identification.IdentifierList;
import net.opengis.sensorML.x101.IdentificationDocument.Identification.IdentifierList.Identifier;
import net.opengis.sensorML.x101.SensorMLDocument;
import net.opengis.sensorML.x101.SensorMLDocument.SensorML;
import net.opengis.sensorML.x101.SensorMLDocument.SensorML.Member;
import net.opengis.sensorML.x101.SystemDocument;
import net.opengis.sensorML.x101.SystemType;
import net.opengis.sensorML.x101.TermDocument.Term;
import net.opengis.sos.x20.SosInsertionMetadataDocument;
import net.opengis.sos.x20.SosInsertionMetadataType;
import net.opengis.swes.x20.InsertSensorDocument;
import net.opengis.swes.x20.InsertSensorType;
import net.opengis.swes.x20.InsertSensorType.Metadata;
import net.opengis.swes.x20.InsertSensorType.ProcedureDescription;
import fr.sedoo.sos.domain.ObservationStation;
import fr.sedoo.sos.domain.Sensor;
import fr.sedoo.sssdata.server.sos.SensorMLConstants;
import fr.sedoo.sssdata.server.sos.XmlBeansUtils;

public class ObservationStationUtils {

	public static String getInsertSensorRequest(ObservationStation station)
	{
		InsertSensorDocument doc = InsertSensorDocument.Factory.newInstance();
		InsertSensorType insertSensor = doc.addNewInsertSensor();
		insertSensor.setService(SensorMLConstants.SOS);
		insertSensor.setVersion(SensorMLConstants.VERSION);
		insertSensor.setProcedureDescriptionFormat("http://www.opengis.net/sensorML/1.0.1");
		ProcedureDescription procedureDescription = insertSensor.addNewProcedureDescription();

		SensorMLDocument docSML = SensorMLDocument.Factory.newInstance();
		SensorML sensorML = docSML.addNewSensorML();
		sensorML.setVersion("1.0.1");
		Member member = sensorML.addNewMember();

		SystemDocument aux = SystemDocument.Factory.newInstance();
		SystemType system = aux.addNewSystem();

		StringOrRefType description = system.addNewDescription();
		description.setStringValue(station.getDescription());

		Identification identification = system.addNewIdentification();
		IdentifierList identifierList = identification.addNewIdentifierList();

		Identifier uniqueId = identifierList.addNewIdentifier();
		uniqueId.setName("URN");
		Term term = uniqueId.addNewTerm();
		term.setDefinition("urn:ogc:def:identifier:OGC:1.0:uniqueID");
		term.setValue(station.getUrn());

		Identifier shortName = identifierList.addNewIdentifier();
		shortName.setName("shortName");
		term = shortName.addNewTerm();
		term.setDefinition("urn:ogc:def:identifier:OGC:1.0:shortName");
		term.setValue(station.getShortName());

		Identifier longName = identifierList.addNewIdentifier();
		longName.setName("longName");
		term = longName.addNewTerm();
		term.setDefinition("urn:ogc:def:identifier:OGC:1.0:longName");
		term.setValue(station.getLongName());

		Identification offeringIdentification = system.addNewIdentification();
		offeringIdentification.setTitle("offerings");
		IdentifierList offeringIdentifierList = offeringIdentification.addNewIdentifierList();
		Identifier offeringIdentifier = offeringIdentifierList.addNewIdentifier();
		Term offeringTerm = offeringIdentifier.addNewTerm();
		offeringTerm.setValue(station.getOffering());
		offeringTerm.setDefinition("urn:ogc:def:identifier:OGC:offeringID");
		offeringIdentifier.setName("Offering for "+station.getUrn());

		CodeType name = system.addNewName();
		name.setStringValue(station.getLongName());

		PointType point = system.addNewSmlLocation().addNewPoint();
		point.setSrsName(station.getSrsName());
		point.addNewCoordinates().setStringValue(station.getLat()+" "+station.getLon());

		//		Capabilities capabilities = system.addNewCapabilities();

		//		DataRecordDocument dataRecordDocument = DataRecordDocument.Factory.newInstance();
		//		DataRecordType dataRecord = dataRecordDocument.addNewDataRecord();
		//		
		//		Field featureOfInterestField = dataRecord.addNewField();
		//		TextDocument textDocument = TextDocument.Factory.newInstance();
		//		TextType text = textDocument.addNewText();
		//		text.setDefinition("FeatureOfInterest identifier");
		//		text.setValue(value)
		//		featureOfInterestField.set(textDocument);
		//		featureOfInterestField.setName("FeatureOfInterestID")
		//				
		//		TimeDocument timeDocument = TimeDocument.Factory.newInstance();
		//		TimeType time = timeDocument.addNewTime();
		//		time.setDefinition("http://www.opengis.net/def/property/OGC/0/SamplingTime");
		//		time.addNewUom().setHref("http://www.opengis.net/def/uom/ISO-8601/0/Gregorian");
		//		featureOfInterest.set(timeDocument);
		//		featureOfInterest.setName("time");
		//		
		//		capabilities.set(dataRecordDocument);

		Metadata metadata = insertSensor.addNewMetadata();
		SosInsertionMetadataDocument insertionMetadataDoc = SosInsertionMetadataDocument.Factory.newInstance();
		SosInsertionMetadataType insertionMetadata = insertionMetadataDoc.addNewSosInsertionMetadata();
		insertionMetadata.setObservationTypeArray(new String[] {"http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement"});
		insertionMetadata.setFeatureOfInterestTypeArray(new String[] {"http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingPoint"});

		metadata.set(insertionMetadataDoc);


		Components components = system.addNewComponents();
		ComponentList componentList = components.addNewComponentList();
		
		Iterator<Sensor> sensorIterator = station.getSensors().iterator();
		while (sensorIterator.hasNext()) 
		{
			Sensor sensor = (Sensor) sensorIterator.next();
			Component component = componentList.addNewComponent();
			component.setName(sensor.getUrn());
		}

		//		if (parameters.get(GosudConstants.TYPE_TSG) != null)
		//		{
		//			Component component = componentList.addNewComponent();
		//			component.setName("urn:gosud:sensor:ship:"+parameters.get(GosudConstants.SHIP_CALL_SIGN)+"::TSG");
		//		}

		/*
		Components components = system.addNewComponents();
		ComponentList componentList = components.addNewComponentList();

		if (parameters.get(GosudConstants.TYPE_TSG) != null)
		{
			Component component = componentList.addNewComponent();
			component.setName("urn:gosud:sensor:ship:"+parameters.get(GosudConstants.SHIP_CALL_SIGN)+"::TSG");
		}

		if (parameters.get(GosudConstants.TYPE_TINT) != null)
		{
			Component component = componentList.addNewComponent();
			component.setName("urn:gosud:sensor:ship:"+parameters.get(GosudConstants.SHIP_CALL_SIGN)+"::TINT");
		}
		 */

		member.set(aux);
		procedureDescription.set(docSML);


		Iterator<String> iterator = station.getObservedProperties().iterator();
		while (iterator.hasNext()) 
		{
			insertSensor.addObservableProperty(iterator.next());
		}
		
		

		//Autre ontologie
		// insertSensor.addObservableProperty("http://marinemetadata.org/cf#sea_water_temperature");

		/*	insertSensor.addObservableProperty(GosudConstants.SSS_OBSERVABLE_PROPERTY);
		insertSensor.addObservableProperty(GosudConstants.SST_OBSERVABLE_PROPERTY);

		Metadata metadata = insertSensor.addNewMetadata();
		SosInsertionMetadataDocument insertionMetadataDoc = SosInsertionMetadataDocument.Factory.newInstance();
		SosInsertionMetadataType insertionMetadata = insertionMetadataDoc.addNewSosInsertionMetadata();
		insertionMetadata.setObservationTypeArray(new String[] {"http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement"});
		insertionMetadata.setFeatureOfInterestTypeArray(new String[] {"http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingCurve"});

		metadata.set(insertionMetadataDoc);
		 */

		return XmlBeansUtils.toString(doc);

	}
	
}
